﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Imaging;

namespace XMLSpriteSheets
{
    public partial class ViewSpriteSheet : Form
    {
        mainFrm Display { get; set; }

        public FileDecoder decoder { get; set; }



        public ViewSpriteSheet(mainFrm frm, Image sheet)
        {
            Display = frm;
            decoder = new FileDecoder(Display);
            InitializeComponent();
        }

        private void fileToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
            this.Dispose();
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
            this.Dispose();
        }

        private void saveToolStripMenuItem_Click(object sender, EventArgs e)
        {
            saveSpriteSheet.CreatePrompt = true;
            saveSpriteSheet.Filter = "Bitmap Image Format (*.bmp)|*.bmp|Exchangable Image Format (*.exif)|*.exif" +
                "|Graphics Interchange Format (*.gif)|*.gif|Joint Photographic Experts Group Format (*.jpeg)|*.jpeg" +
                "|Portable Network Graphics Format (*.png)|*.png|Tagged Image File Format (*.tiff)|*.tiff" +
                "|Window Meta File Format (*.wmf)|*.wmf|Windows Icon Image Format (*.ico)|*.ico";
            saveSpriteSheet.FilterIndex = 2;
            saveSpriteSheet.Title = "Save " + Display.xmlSheet + " as. ";

            if (saveSpriteSheet.ShowDialog() == DialogResult.OK)
            {
                // What they pick ?
                switch (saveSpriteSheet.FilterIndex)
                {
                        /*
                         * 1 = bmp
                         * 2 = exif
                         * 3 = gif
                         * 4 = jpeg
                         * 5 = png
                         * 6 = tiff
                         * 7 = wmf
                         * 8 = ico
                         */
                    case 1: { decoder.SaveAs(ImageFormat.Bmp, saveSpriteSheet.FileName); } // We chose bmp
                        break;
                    case 2: { decoder.SaveAs(ImageFormat.Exif, saveSpriteSheet.FileName); } // We chose eixf
                        break;
                    case 3: { decoder.SaveAs(ImageFormat.Gif, saveSpriteSheet.FileName); } // We chose gif
                        break;
                    case 4: { decoder.SaveAs(ImageFormat.Jpeg, saveSpriteSheet.FileName); } // We chose jpeg
                        break;
                    case 5: { decoder.SaveAs(ImageFormat.Png, saveSpriteSheet.FileName); } // We chose png
                        break;
                    case 6: { decoder.SaveAs(ImageFormat.Tiff, saveSpriteSheet.FileName); } // We chose tiff
                        break;
                    case 7: { decoder.SaveAs(ImageFormat.Wmf, saveSpriteSheet.FileName); } // We chose wmf
                        break;
                    case 8: { decoder.SaveAs(ImageFormat.Icon, saveSpriteSheet.FileName); } // We chose Icon
                        break;
                    default:
                        // Hmm we dont have any other choices, so should never get here.
                        MessageBox.Show("We cant save to that filetype.",
                            "Attempt to save as unsupported file!!",
                            MessageBoxButtons.OK,
                            MessageBoxIcon.Stop);
                        break;
                }
            }

            
        }

        private void ViewSpriteSheet_Load(object sender, EventArgs e)
        {
            
                //MessageBox.Show("I see image " + Display.xmlSheet);
                //Its there, load it.
            imageBox.Image = Image.FromFile(Display.xmlSheet); // Load the Image
            string newtitle = Display.xmlSheet.Substring(Display.xmlSheet.LastIndexOf("\\") + 1);
            this.Text = newtitle + " " + imageBox.Image.PhysicalDimension;
            //TODO Beef this form up
            
            
            
            
        }

       
    }
}
